package com.imooc.mall.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imooc.mall.common.Constant;
import com.imooc.mall.exception.ImoocMallException;
import com.imooc.mall.exception.ImoocMallExceptionEnum;
import com.imooc.mall.model.dao.ProductMapper;
import com.imooc.mall.model.pojo.Product;
import com.imooc.mall.model.request.AddProductReq;
import com.imooc.mall.model.request.ProductListReq;
import com.imooc.mall.model.request.UpdateProductReq;
import com.imooc.mall.query.ProductListQuery;
import com.imooc.mall.service.CategoryService;
import com.imooc.mall.service.ProductService;
import com.imooc.mall.vo.CategoryVO;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service()
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductMapper productMapper;

    @Autowired
    CategoryService categoryService;

    @Override
    public void add(AddProductReq addProductReq) {
        Product product = new Product();
        BeanUtils.copyProperties(addProductReq, product);
        // 商品是否存在
        final Product productOld = productMapper.selectByName(product.getName());

        if (productOld != null) {
            throw new ImoocMallException(ImoocMallExceptionEnum.NAME_EXISTED);
        }

        final int i = productMapper.insertSelective(product);

        if (i == 0) {
            throw new ImoocMallException(ImoocMallExceptionEnum.CREATE_FAILED);
        }
    }

    @Override
    public void update(UpdateProductReq updateProductReq) {
        Product product = new Product();
        final Product productOld = productMapper.selectByName(updateProductReq.getName());

        if (productOld != null && updateProductReq.getId().equals(productOld.getId())) {
            throw new ImoocMallException(ImoocMallExceptionEnum.NAME_EXISTED);
        }

        BeanUtils.copyProperties(updateProductReq, product);
        final int i = productMapper.updateByPrimaryKeySelective(product);
        if (i < 0) {
            throw new ImoocMallException(ImoocMallExceptionEnum.UPDATE_FAILED);
        }
    }
    @Override
    public void delete(Integer id) {

        final Product productOld = productMapper.selectByPrimaryKey(id);
        if (productOld == null ) {
            throw new ImoocMallException(ImoocMallExceptionEnum.DELETE_FAILED);
        }
        final int i = productMapper.deleteByPrimaryKey(id);
        if (i < 0) {
            throw new ImoocMallException(ImoocMallExceptionEnum.DELETE_FAILED);
        }
    }

    @Override
    public void batchUpdateStatus(Integer[] ids, Integer status) {
        final int i = productMapper.batchUpdateStatus(ids, status);
        if (i <= 0) {
            throw new ImoocMallException(ImoocMallExceptionEnum.UPDATE_FAILED);
        }
    }

    @Override
    public PageInfo listForAdmin(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        final List<Product> productList = productMapper.selectListForAdmin();

        return new PageInfo<>(productList);
    }

    @Override
    public Product detail(Integer id) {
        return productMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo list(ProductListReq productListReq) {
        // 构建 query 对象
        final ProductListQuery listQuery = new ProductListQuery();
        // 搜索处理
        if (!StringUtils.isEmpty(productListReq.getKeyword())) {
            listQuery.setKeyword("%" + productListReq.getKeyword() + "%");
        }
        // 目录处理。查询某个某个目录下的商品，不仅需要查询该目录下的，还要把所有子目录的所有商品都查询出来，所有要一个目录的list
        if (productListReq.getCategoryId() != null) {
            final List<CategoryVO> categoryVOList = categoryService.listCategoryForCustomer(productListReq.getCategoryId());
            ArrayList<Integer> arrayList = new ArrayList<>();
            getCategoryIds(categoryVOList, arrayList);
            listQuery.setCategoryIds(arrayList);
        }
        // 排序, 处理。只对指定字段进行排序
        final String orderBy = productListReq.getOrderBy(); // 前端传递的排序参数
        if (Constant.productOrderBy.PRICE_DESC_ASC.contains(orderBy)) {
            PageHelper.startPage(productListReq.getPageNum(), productListReq.getPageSize(), orderBy);
        } else {
            PageHelper.startPage(productListReq.getPageNum(), productListReq.getPageSize());
        }

        List<Product> productList = productMapper.selectList(listQuery.getKeyword(), listQuery.getCategoryIds());

        return new PageInfo<>(productList);
    }

    private void getCategoryIds(
            List<CategoryVO> categoryVOList,
            ArrayList<Integer> categoryIds
    ) {
        for (final CategoryVO categoryVO : categoryVOList) {
            if (categoryVO != null) {
                categoryIds.add(categoryVO.getId());
                getCategoryIds(categoryVO.getChildCategory(), categoryIds);
            }
        }
    }
}
